/*DccPB1s1Oh183l+CTCyAf1funHahF6OVPl/EMJBx/nOv9J9t1EYoaEYzDVazDTO4*/
/*
 * @Descripttion: 关联客户弹窗相关方法(卡片/列表公用)
 * @version:
 * @Author: Chendch
 * @Date: 2020-12-29 17:29:39
 * @LastEditors: Chendch
 * @LastEditTime: 2021-03-23 14:19:42
 */
import {promptBox,ajax, toast } from "nc-lightapp-front";
import { validateFormulaSetting  } from './SupplierUtils';
import {Urls} from "../main/CONSTANCE";
function str2Object(str) {
    let obj = {};
    str.split(",").forEach( item => {
        let key,value;
        key = item.split(":")[0].replace(/^\{/gi, "");
        value= item.split(":")[1].replace(/\}$/gi, "");
        obj[key] = value;
    });
    return obj;
}
/**
     * 关联客户确认按钮点击事件
*/
function onAssociateCustSureClick(callback,destroyHotKeys) {
    var me = this;
    if(!!me.state.otherParam ){
        //存在已关联的客户信息
        let customer = typeof me.state.otherParam == "string" ? JSON.parse(me.state.otherParam) : me.state.otherParam;
        if(customer && customer.hasOwnProperty('customer')){
            customer = customer.customer;
        }
        //修改交互方式，由原来的dialog方式改成目前的promptBox的方式
        destroyHotKeys(true,()=>{
            promptBox({
                color:'warning',
                title:this.Lang['10140SUG-000077'],/* 国际化处理： 询问*/
                //content:this.inlt && this.inlt.get('10140SUG-000153',{custCode:customer.code,custName:customer.name}),/* 国际化处理： 当前供应商已经关联编码[{${customer.code}]名称[${customer.name}]的客户，是否取消已有客商关系，重新建立客商关系？,当前供应商已经关联编码,名称,的客户,是否取消已有客商关系,重新建立客商关系*/
                content:this.inlt && this.inlt.get('10140SUG-000262'),/* 国际化处理： 解除或重建客商关系可能会对引用客商参照的业务数据产生影响,是否确定操作?*/
                beSureBtnClick:()=>{
                    //关联客户点击确认按钮事件
                    this.onAssociateCustPrompSureBtnClick(callback,destroyHotKeys);
                },
                cancelBtnClick:()=>{destroyHotKeys(false);}
            })
        })
    }else{
        //不存在已关联客户信息
        destroyHotKeys(true,()=>{this.onAssociateCustPrompSureBtnClick(callback,destroyHotKeys);});
    }
}

 /**
     *  关联客户取消按钮 点击事件
*/
function onAssociateCustCancelBtnClick(callback,destroyHotKeys) {
    destroyHotKeys(true,()=>{
        promptBox({
            color:'warning',
            title:this.Lang['10140SUG-000067'],/* 国际化处理： 确认取消*/
            content:this.Lang['10140SUG-000068'],/* 国际化处理： 是否确认要取消？*/
            beSureBtnClick:()=>{
                destroyHotKeys(false,()=>{
                    this.Dialog.closeDialog(this.onAfterDialogClose);
                });
            },
            cancelBtnClick:()=>{
                destroyHotKeys(false);
            }
        })

    })
}

function onAssociateCustPrompSureBtnClick(callback,destroyHotKeys) {
    if(!this.props.form.isCheckNow('associcust')){
        destroyHotKeys(false);
        return;
    }
    let _that = this;
    let associateData = this.props.form.getAllFormValue("associcust");
    let pk_customer = this.props.form.getAllFormValue("associcust").rows[0].values.corcustomer.value;
    let pk_supplier = this.props.form.getAllFormValue("associcust").rows[0].values.pk_supplier.value;
    let pk_supplierVO = this.props.form.getAllFormValue("associcust").rows[0].values;
    associateData.areacode = 'associcust';
    let formdata = {
        form:{
            model:associateData,
            pageid:_that.props.pagecode
        },
        inCard:_that.state.openCard
    };
    validateFormulaSetting.call(this,formdata,()=>{
        ajax({
            url:Urls.saveAssociateCustUrl,
            data : {
                form:{
                    model:associateData,
                    pageid:_that.props.pagecode
                },
                inCard:_that.state.openCard
            },
            success:(res)=>{
                if(res.success){
                    destroyHotKeys(false,()=>{
                        this.Dialog.closeDialog(()=>{
                            this.onAfterDialogClose(()=>{
                                this.state.openCard?res.data&&this.setState({
                                    selectedRecords:[pk_supplierVO],
                                    relateQueryId: pk_supplier,
                                },()=>{
                                    this.initPageData(this.setCardValue,this.setCardStatus);
                                }):this.onSearch({checagepage:true},this.setTableData);
                            });
                        });

                        toast({title:this.Lang['custandsup001'],color:'success'});/* 国际化处理： 后台任务正在执行，请稍后在消息中心查询结果！*/
                    })
                }
            },
            error:(e)=>{destroyHotKeys(false,()=>{
                //不知道平章这里怎么弄得导致后端抛异常的提示框弹不出来，只能前端再提示了
                toast({content:e.message,color:'danger'});
            });}
        })

    },{'associcust':'form'},'form');
}

function associateCustContent() {
    const onAfterFormEvent = function(){}
    const {form} = this.props;
    const {createForm} = form;
    return createForm('associcust',{onAfterEvent: onAfterFormEvent.bind(this)});
}


export {
    onAssociateCustSureClick,
    onAssociateCustCancelBtnClick,
    onAssociateCustPrompSureBtnClick,
    associateCustContent
}
/*DccPB1s1Oh183l+CTCyAf1funHahF6OVPl/EMJBx/nOv9J9t1EYoaEYzDVazDTO4*/
